package ray.mgocc;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Properties;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 戦績。scoreのcollection.
 */
public class Record extends ArrayList<Score>{
    private final static Log log = LogFactory.getLog(Record.class);
    public final static boolean TOTAL = false;
    public final static boolean WEEK = true;
    public boolean add(Score score){
        if(null == score){
            throw new IllegalArgumentException("score is null.");
        }
        log.debug("record:"+score.getName()+" week:"+score.isWeekly());
        return super.add(score);
    }
    public Score getTotal(String name){
        return getScore(name,TOTAL);
    }
    public Score getWeek(String name){
        return getScore(name,WEEK);
    }
    public Score getScore(String name,boolean week){
        if(null==name){
            throw new IllegalArgumentException("name is null.");
        }
        Score hit = null;
        for(Score sample:this){
            if(week == sample.isWeekly() &&
               name.equals(sample.getName())){
                hit = sample;
            }
        }
        return hit;
    }
    public Object getValue(String key){
        log.trace("key="+key);
        Object value = null;
        try{
            Scanner scanner = new Scanner(key).useDelimiter("\\.");
            boolean period;
            String strPeriod = scanner.next();
            if("WEEK".equals(strPeriod)){
                period = WEEK;
            }else if("TOTAL".equals(strPeriod)){
                period = TOTAL;
            }else{
                throw new RuntimeException("illegal period="+strPeriod);
            }

            String scoreName = scanner.next();
            Score score = getScore(scoreName,period);

            String valueName = scanner.next();
            if(scanner.hasNext()){
                String detailName = scanner.next();
                value = score.getRule().get(valueName).get(detailName);
            }else{
                value = score.get(valueName);
                if(value instanceof RuleScore){
                    throw new RuntimeException("detail name not specified.");
                }
            }

            if(null==value){
                throw new NullPointerException(key+" is "+value);
            }
        }catch(RuntimeException e){
            throw new RuntimeException("fail:"+key,e);
        }
        return value;
    }
    public Properties getAll(){
        Properties all = new Properties();
        for(Score score:this){
            String key;
            key = (score.isWeekly()?"WEEK":"TOTAL")+".";
            key += score.getName()+".";
            Properties scoreSet = score.getAll();
            for(Map.Entry entry:scoreSet.entrySet()){
                all.setProperty(key+(String)entry.getKey(),(String)entry.getValue());
            }
        }
        return all;
    }
}
